package com.vip8.recorder.service.aop;


import com.vip8.recorder.service.utils.AopUtil;
import com.vip8.recorder.service.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 接口日志统一处理
 */

@Aspect
@Slf4j
@Component
public class ServiceLog {

    @Pointcut("execution(* com.vip8.recorder.service.service.impl.*.*(..))  ")
    public void cutPoint() {

    }

    @Around("cutPoint()")
    public Object process(ProceedingJoinPoint joinPoint) throws Throwable {
        //只处理方法
        MethodSignature methodSignature = AopUtil.getMethodSignature(joinPoint);
        if (methodSignature == null) {
            return joinPoint.proceed();
        }

        Object args = joinPoint.getArgs();
        String paramJson = JsonUtil.toJsonString(args);
        String executionMethod = AopUtil.getMethodName(joinPoint);
        log.info("{} ------args: {}", executionMethod, paramJson);

        Object result;

        try {
            result = joinPoint.proceed();
        } catch (Throwable e) {
            log.info("{} -----throwableHappens: \n{} ", executionMethod, AopUtil.getStaceTrace(e));
            throw e;
        }
        log.info("{} ------result: {}", executionMethod, JsonUtil.toJsonString(result));
        return result;
    }
}
